热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#|Convert。ToInt64(字符串,表单提供者)方法

C#|Convert。ToInt64(字符串,表单提供者)方法

C# | Convert。ToInt64(字符串,表单提供者)方法

原文:https://www . geesforgeks . org/c-sharp-convert-to int 64 string-iformatprovider-method/

此方法用于使用指定的区域性特定格式信息,将数字的指定字符串表示形式转换为等效的 64 位有符号整数。

语法:

public static long ToInt64 (string value, IFormatProvider provider);

参数:


  • 值:是包含要转换的数字的字符串。

  • 提供程序:它是一个提供区域性特定格式信息的对象。

返回值:该方法返回一个十进制数,它相当于中的数字,如果空值,则返回 0(零)。

异常:


  • 格式异常:如果不是由后跟数字序列(0 到 9)的可选符号组成。

  • overoverowexception:如果代表小于最小值或大于最大值的数字。

以下程序说明了转换的使用。ToInt64(字符串,表单提供者)方法:

例 1:

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value"
                    + " from a specified string ");
        for (int j = 0; j < values.Length; j++) 
        {
            get(values[j], cultures);
        }
    }
    catch (FormatException e)
    {
        Console.WriteLine("\n");
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e)
    {
        Console.WriteLine("\n");
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to specified long
    long val = Convert.ToInt64(s, cultures);
    // display the converted long value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value from a specified string 12345, 12345, -12345,

例 2:格式异常

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value"
                    + " of specified strings: ");
        for (int j = 0; j < values.Length; j++) 
        {
            get(values[j], cultures);
        }
        Console.WriteLine("\n");
        string s = "123 456, 789";
        Console.WriteLine("format of s is invalid ");
        // converting string to specified char
        long val = Convert.ToInt64(s, cultures);
        // display the converted char value
        Console.Write(" {0}, ", val);
    }
    catch (FormatException e)
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e) 
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to
    // specified long value
    long val = Convert.ToInt64(s, cultures);
    // display the converted
    // decimal value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value of specified strings: 12345, 12345, -12345,
format of s is invalid
Exception Thrown: System.FormatException

例 3: 适用于飞越异常

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value "
                 + "of specified strings: ");
        for (int j = 0; j < values.Length; j++)
        {
            get(values[j], cultures);
        }
        Console.WriteLine("\n");
        string s = "-7922816251426433759354395033500000";
        Console.WriteLine("s is less than the MinValue");
        // converting string to specified long
        long val = Convert.ToInt64(s, cultures);
        // display the converted char value
        Console.Write(" {0}, ", val);
    }
    catch (FormatException e)
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e) 
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to
    // specified long value
    long val = Convert.ToInt64(s, cultures);
    // display the converted long value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value of specified strings: 12345, 12345, -12345,
s is less than the MinValue
Exception Thrown: System.OverflowException

参考:


  • https://docs . Microsoft . com/en-us/dotnet/API/system . convert . toint 64?view = net framework-4 . 7 . 2 # System _ Convert _ toint 64 _ System _ String _ System _ IFormatProvider _


推荐阅读
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
author-avatar
展翅翱翔512
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有